<?php

namespace App\Http\Controllers\Guard\Article;

use App\Http\Controllers\Common\GuardController;
use App\Models\Article\ArticleClassify;
use App\Models\Constant\ArticleConstant;
use App\Models\Constant\Constant;
use Illuminate\Http\Request;


class ArticleClassifyController extends GuardController {
    public function lst(Request $request) {

        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',Constant::PAGE_SIZE);
        $conditions = $request->input();
        $records = (new ArticleClassify())->getLst($conditions)
            ->with(['getGuardUser:id,name'])
            ->withCount(['getArticle'])
            ->paginate($pageSize,['*'],'page',$page);

        $mapBelongTo  = [];
        foreach (ArticleConstant::MAP_BELONG_TO as $k=>$v) {
            $mapBelongTo[] = ['label'=>$v,'value'=>$k];
        }

        return responseSuccess($records->items(),$records->total(),100000,compact('mapBelongTo'));
    }

    public function add(Request $request) {

        try{
            $id = $request->input('id',0);
            $info = $request->input();
            $info['belong_to_id'] = $info['belong_to'];
            $info['belong_to'] = ArticleConstant::MAP_BELONG_TO[$info['belong_to_id']]??'未知';

            $token = $request->header('token');
            if(empty($token)) return responseFail('获取token失败！');
            $guard_user_id = checkJWT($token)['id']??0;
            if($guard_user_id<=0){
                return responseFail('获取当前用户失败！');
            }
            $info['guard_user_id'] = $guard_user_id;


            ArticleClassify::updateOrCreate(compact('id'),$info);
            return responseSuccess();
        }catch(\Exception $e){
            $tip = $e->getFile().':'.$e->getLine().':'.$e->getMessage();
            return responseFail($tip);
        }
    }

    public function getRecord(Request $request) {

        return $this->getRecordByIds(ArticleClassify::class, $request->input('id','')); // TODO: Change the autogenerated stub
    }

    public function del(Request $request) {
        $ids = $request->input('id');
        if(!is_array($ids)){
            $ids = [$ids];
        }
        $status = $request->input('status');
        ArticleClassify::whereIn('id',$ids)->update(compact('status'));
        return responseSuccess();
    }




}
